{% extends "auth/base.html" %}

{% block title %}个人资料 - Flask权限管理系统{% endblock %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="col-md-4">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fas fa-user"></i> 个人资料</h5>
                </div>
                <div class="card-body text-center">
                    <div class="mb-3">
                        <img src="https://picsum.photos/seed/{{ user.username }}/150/150.jpg" 
                             class="rounded-circle img-fluid" alt="用户头像">
                    </div>
                    <h4>{{ user.username }}</h4>
                    <p class="text-muted">{{ user.email }}</p>
                    <div class="d-flex justify-content-center mb-3">
                        {% if user.is_active %}
                        <span class="badge bg-success me-1">活跃</span>
                        {% else %}
                        <span class="badge bg-danger me-1">已禁用</span>
                        {% endif %}
                        
                        {% if user.is_administrator() %}
                        <span class="badge bg-danger">管理员</span>
                        {% endif %}
                    </div>
                    <div class="d-grid gap-2">
                        <a href="{{ url_for('auth.change_password') }}" class="btn btn-outline-primary">
                            <i class="fas fa-lock"></i> 修改密码
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-info-circle"></i> 账户信息</h5>
                </div>
                <div class="card-body">
                    <ul class="list-group list-group-flush">
                        <li class="list-group-item d-flex justify-content-between">
                            <span>用户ID</span>
                            <span>{{ user.id }}</span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between">
                            <span>注册时间</span>
                            <span>{{ user.created_at.strftime('%Y-%m-%d') if user.created_at else '未知' }}</span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between">
                            <span>最后登录</span>
                            <span>{{ user.last_login.strftime('%Y-%m-%d %H:%M') if user.last_login else '从未登录' }}</span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between">
                            <span>登录次数</span>
                            <span>{{ user.login_count or 0 }}</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        
        <div class="col-md-8">
            <div class="card">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0"><i class="fas fa-user-tag"></i> 用户角色</h5>
                </div>
                <div class="card-body">
                    {% if user.roles %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>角色名称</th>
                                    <th>角色描述</th>
                                    <th>权限数量</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for role in user.roles %}
                                <tr>
                                    <td>
                                        <span class="badge bg-primary">{{ role.name }}</span>
                                    </td>
                                    <td>{{ role.description or '无描述' }}</td>
                                    <td>{{ role.permissions|length }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <p class="text-muted">您尚未分配任何角色</p>
                    {% endif %}
                </div>
            </div>
            
            <div class="card mt-3">
                <div class="card-header bg-warning text-dark">
                    <h5 class="mb-0"><i class="fas fa-key"></i> 用户权限</h5>
                </div>
                <div class="card-body">
                    {% set permissions = user.get_permissions() %}
                    {% if permissions %}
                    <div class="row">
                        {% for permission in permissions %}
                        <div class="col-md-6 mb-2">
                            <div class="d-flex align-items-center">
                                <i class="fas fa-check-circle text-success me-2"></i>
                                <span>{{ permission.name }}</span>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                    {% else %}
                    <p class="text-muted">您没有任何特殊权限</p>
                    {% endif %}
                </div>
            </div>
            
            {% if user.is_administrator() %}
            <div class="card mt-3">
                <div class="card-header bg-danger text-white">
                    <h5 class="mb-0"><i class="fas fa-cogs"></i> 管理员操作</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6 mb-2">
                            <a href="{{ url_for('admin.dashboard') }}" class="btn btn-danger w-100">
                                <i class="fas fa-tachometer-alt"></i> 管理面板
                            </a>
                        </div>
                        <div class="col-md-6 mb-2">
                            <a href="{{ url_for('admin.users') }}" class="btn btn-outline-danger w-100">
                                <i class="fas fa-users"></i> 用户管理
                            </a>
                        </div>
                        <div class="col-md-6 mb-2">
                            <a href="{{ url_for('admin.roles') }}" class="btn btn-outline-danger w-100">
                                <i class="fas fa-user-tag"></i> 角色管理
                            </a>
                        </div>
                        <div class="col-md-6 mb-2">
                            <a href="{{ url_for('admin.permissions') }}" class="btn btn-outline-danger w-100">
                                <i class="fas fa-key"></i> 权限管理
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}